package com.bee.littlebeetravel.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.apkfuns.logutils.LogUtils;
import com.bee.littlebeetravel.R;
import com.bee.littlebeetravel.adapter.Message_SV_Adapter;
import com.bee.littlebeetravel.utils.ToastUtil;
import com.yanzhenjie.recyclerview.OnItemClickListener;
import com.yanzhenjie.recyclerview.OnItemMenuClickListener;
import com.yanzhenjie.recyclerview.SwipeMenu;
import com.yanzhenjie.recyclerview.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.SwipeMenuItem;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import q.rorbin.badgeview.QBadgeView;

public class FriendMessageFragment extends Fragment {

    private Unbinder unbinder;

    @BindView(R.id.tv_club)
    TextView tv_club;
    @BindView(R.id.tv_swarm)
    TextView tv_swarm;
    @BindView(R.id.tv_personal)
    TextView tv_personal;
    @BindView(R.id.rl_empty)
    RelativeLayout rl_empty;
    @BindView(R.id.srv_message)
    SwipeRecyclerView srv_message;
    private Message_SV_Adapter message_sv_adapter;
    List<String> datas;

    QBadgeView qBadgeView_swarm;
    QBadgeView qBadgeView_personal;

    @OnClick({R.id.tv_club, R.id.tv_swarm, R.id.tv_personal})
    public void click(View v) {
        switch (v.getId()) {
            case R.id.tv_club:
                tv_club.setBackgroundResource(R.drawable.corner_org5_left);
                tv_club.setTextColor(getActivity().getResources().getColor(R.color.white));
                tv_swarm.setBackgroundResource(R.drawable.corner_org5_bord_middle);
                tv_swarm.setTextColor(getActivity().getResources().getColor(R.color.org500));
                tv_personal.setBackgroundResource(R.drawable.corner_org5_bord_right);
                tv_personal.setTextColor(getActivity().getResources().getColor(R.color.org500));
                qBadgeView_swarm.setVisibility(View.VISIBLE);
                qBadgeView_personal.setVisibility(View.VISIBLE);
                datas.clear();
                for (int i = 0; i < 10; i++) {
                    datas.add("itme " + i);
                }
                srv_message.setVisibility(View.VISIBLE);
                rl_empty.setVisibility(View.GONE);
                message_sv_adapter.notifyDataSetChanged();
                break;
            case R.id.tv_swarm:
                tv_club.setBackgroundResource(R.drawable.corner_org5_bord_left);
                tv_club.setTextColor(getActivity().getResources().getColor(R.color.org500));
                tv_swarm.setBackgroundResource(R.color.org500);
                tv_swarm.setTextColor(getActivity().getResources().getColor(R.color.white));
                tv_personal.setBackgroundResource(R.drawable.corner_org5_bord_right);
                tv_personal.setTextColor(getActivity().getResources().getColor(R.color.org500));
                qBadgeView_personal.setVisibility(View.VISIBLE);
                qBadgeView_swarm.setVisibility(View.INVISIBLE);
                datas.clear();
                for (int i = 0; i < 5; i++) {
                    datas.add("itme " + i);
                }
                message_sv_adapter.notifyDataSetChanged();
                srv_message.setVisibility(View.VISIBLE);
                rl_empty.setVisibility(View.GONE);
                break;
            case R.id.tv_personal:
                tv_club.setBackgroundResource(R.drawable.corner_org5_bord_left);
                tv_club.setTextColor(getActivity().getResources().getColor(R.color.org500));
                tv_swarm.setBackgroundResource(R.drawable.corner_org5_bord_middle);
                tv_swarm.setTextColor(getActivity().getResources().getColor(R.color.org500));
                tv_personal.setBackgroundResource(R.drawable.corner_org5_right);
                tv_personal.setTextColor(getActivity().getResources().getColor(R.color.white));
                qBadgeView_swarm.setVisibility(View.VISIBLE);
                qBadgeView_personal.setVisibility(View.INVISIBLE);

                srv_message.setVisibility(View.GONE);
                rl_empty.setVisibility(View.VISIBLE);
                break;
        }
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_friend_message, container, false);
        unbinder = ButterKnife.bind(this, v);
        initBadgeView();
        initTestData(10);
        return v;
    }

    /**
     * 顶部角标初始化
     */
    public void initBadgeView() {
        qBadgeView_swarm = new QBadgeView(getActivity());
        qBadgeView_swarm.bindTarget(tv_swarm).setBadgeNumber(-1).setGravityOffset(8, true).setShowShadow(false);
        qBadgeView_personal = new QBadgeView(getActivity());
        qBadgeView_personal.bindTarget(tv_personal).setBadgeNumber(-1).setGravityOffset(8, true).setShowShadow(false);
    }

    // 测试数据
    public void initTestData(int number) {
        rl_empty.setVisibility(View.GONE);
        datas = new ArrayList<>();
        for (int i = 0; i < number; i++) {
            datas.add("item " + i);
        }
        message_sv_adapter = new Message_SV_Adapter(getActivity(), datas);
        //设置LayoutManager为LinearLayoutManager
        srv_message.setLayoutManager(new LinearLayoutManager(getActivity()));
        // 创建侧滑菜单
        SwipeMenuCreator mSwipeMenuCreator = new SwipeMenuCreator() {
            @Override
            public void onCreateMenu(SwipeMenu leftMenu, SwipeMenu rightMenu, int position) {
                int height = ViewGroup.LayoutParams.MATCH_PARENT;
                SwipeMenuItem addItem = new SwipeMenuItem(getActivity())
                        .setBackgroundColorResource(R.color.red4343)
                        .setText("删除")
                        .setTextColor(Color.WHITE)
                        .setWidth(200)
                        .setTextSize(18)
                        .setHeight(height);
                rightMenu.addMenuItem(addItem); // 添加菜单到右侧。
            }
        };
        // 设置监听器。
        srv_message.setSwipeMenuCreator(mSwipeMenuCreator);
        // 监听删除点击事件
        OnItemMenuClickListener mItemMenuClickListener = new OnItemMenuClickListener() {
            @Override
            public void onItemClick(SwipeMenuBridge menuBridge, int position) {
                // 任何操作必须先关闭菜单，否则可能出现Item菜单打开状态错乱。
                menuBridge.closeMenu();
                // 进行删除操作
                datas.remove(position);
                // 没有数据显示无数据信息
                if (datas.size() == 0) {
                    rl_empty.setVisibility(View.VISIBLE);
                }
                message_sv_adapter.notifyDataSetChanged();
            }
        };
        // 菜单点击监听。
        srv_message.setOnItemMenuClickListener(mItemMenuClickListener);
        // 条目点击事件
        srv_message.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int adapterPosition) {
                ToastUtil.showShort(getActivity(), "adapterPosition = " + adapterPosition);
            }
        });
        //设置Adapter
        srv_message.setAdapter(message_sv_adapter);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (unbinder != null) {
            unbinder.unbind();
        }
    }
}